from text_util import *
from sys_util import read_config_value
import os
import json

config_file = 'config.ini'
json_file = 'prompt.json'

os.environ["http_proxy"] = read_config_value(config_file, 'Proxy', 'http_proxy')
os.environ["https_proxy"] = read_config_value(config_file, 'Proxy', 'https_proxy')

def load_data_from_file(file_path):
    with open(file_path, 'r', encoding='utf-8') as file:
        data = json.load(file)
    return data

def main():
    delimiter = "####"
    notice = f"(The user input message will be delimited with {delimiter} characters.)"

    # 获取当前脚本的绝对路径
    script_path = os.path.abspath(__file__)
    # 获取当前脚本所在文件夹的路径
    script_dir = os.path.dirname(script_path)
    # 构建data.json的绝对路径
    data_path = os.path.join(script_dir, json_file)
    full_data = load_data_from_file(data_path)

    display_options(full_data)
    print(f"\n请输入对应数字来选择所需功能:", end="")
    choice = get_user_choice(min_num=1, max_num=len(full_data))
    data = full_data[list(full_data.keys())[choice-1]]

    system_content = data.get("system_content", "")
    system_content = system_content + "\n" + notice
    user_assistant_contents_e2c = data.get("user_assistant_contents", [])

    # 总的长度为 user_content + assistant_content + system_content
    # user_content 和 assistant 数量一样
    total_len = len(user_assistant_contents_e2c) * 2 + 1

    message_list = [content2dic(role="system", content=system_content)]

    for user_content, assistant_content in user_assistant_contents_e2c:
        message_list.extend([
            content2dic(role="user", content=user_content, delimiter="####"),
            content2dic(role="assistant", content=assistant_content)
        ])

    is_multi_line = ('True' == read_config_value(config_file, 'Options', 'is_multi_line'))
    while True:
        print("=================================================")
        user_input_dic = get_user_content2dic(delimiter = delimiter, is_multi_line=is_multi_line)
        # print(message_list)
        assistant_content = generate_assistant_content(message_list, user_input_dic, total_len=total_len)
        print()

if __name__ == '__main__':
    main()

